
set more off
cap log close

*cd "C:\Users\dainn\Dropbox\Work\Pleng\Article Version"
cd "C:\Users\wie-dainn\Dropbox\Work\Pleng\Article Version"
*cd "C:\Users\wie-dainn\Dropbox\Work\Pleng\Analysis\Chapter1"

/*
********************************************************************************
*Preparation*
********************************************************************************
use "Data\data.dta", clear
drop if year==2012
keep if age>=15 & age<=24

********************************************************************************
*Acquire coefficients to compare from Table 2
********************************************************************************

drop if year==2012

*(1)
reghdfe schdrop taup1* manup* sex member if age>=15 & age<=24 [pw=weight], absorb(year province) cluster(PSU_NO)

*(2)
reghdfe schdrop taup1* manup* sex member if age>=15 & age<=24 & area==0 [pw=weight], absorb(year province) cluster(PSU_NO)

*(3)
reghdfe schdrop taup1* manup* sex member if age>=15 & age<=24 & area==1 [pw=weight], absorb(year province) cluster(PSU_NO)


********************************************************************************
*Permutation Test
********************************************************************************

* ESTABLISH A FIXED INITIAL ORDER FOR THE DATA
bysort year: gen id = _n
bysort year: gen temp_id = _n
save "Permutation\master.dta", replace

keep year id taup13 taup14 taup15 taup16 
save "Permutation\initial.dta", replace

forval y = 1(1)10000{
use "Permutation\initial.dta", clear
set seed `y'
gen double shuffle=runiform()

bysort year (shuffle): gen temp_id = _n
drop shuffle
rename taup13 ftaup13
rename taup14 ftaup14
rename taup15 ftaup15
rename taup16 ftaup16

save "Permutation\shuffle.dta", replace
clear

use "Permutation\master.dta", clear
merge 1:1 year temp_id using "Permutation\shuffle.dta"

keep if _merge==3
drop _merge


*Table 2 (2): Rural minority
reghdfe schdrop ftaup1* manup* sex member if age>=15 & age<=24 & area==0 & year!=2012 [pw=weight], absorb(year province) cluster(PSU_NO)

gen coef_reg1=_b[ftaup13] in 1
gen coef_reg2=_b[ftaup14] in 1
gen coef_reg3=_b[ftaup15] in 1
gen coef_reg4=_b[ftaup16] in 1


*Table 2 (3): Urban minority
reghdfe schdrop ftaup1* manup* sex member if age>=15 & age<=24 & area==1 & year!=2012 [pw=weight], absorb(year province) cluster(PSU_NO)

gen coef_reg5=_b[ftaup13] in 1
gen coef_reg6=_b[ftaup14] in 1
gen coef_reg7=_b[ftaup15] in 1
gen coef_reg8=_b[ftaup16] in 1


keep coef_reg1 coef_reg2 coef_reg3 coef_reg4 coef_reg5 coef_reg6 coef_reg7 coef_reg8 
drop if coef_reg1==.
drop if coef_reg2==.
drop if coef_reg3==.
drop if coef_reg4==.
drop if coef_reg5==.
drop if coef_reg6==.
drop if coef_reg7==.
drop if coef_reg8==.

g ranshuffle = `y'

save "Permutation\fals_`y'.dta", replace
}


clear
use "Permutation\fals_1.dta", clear
forval x = 2(1)10000{
	append using Permutation\fals_`x'.dta
}

save "Permutation\falsdata.dta", replace
*/

use "Permutation\falsdata.dta", clear
sum coef_reg1 if coef_reg1<-0.084
gen pvalue1=(r(N)/10000)*2
dis pvalue1

sum coef_reg2 if coef_reg2<-0.077
gen pvalue2=(r(N)/10000)*2
dis pvalue2

sum coef_reg3 if coef_reg3<-0.161
gen pvalue3=(r(N)/10000)*2
dis pvalue3

sum coef_reg4 if coef_reg4<-0.104
gen pvalue4=(r(N)/10000)*2
dis pvalue4

sum coef_reg5 if coef_reg5>0.023
gen pvalue5=(r(N)/10000)*2
dis pvalue5

sum coef_reg6 if coef_reg6>0.027
gen pvalue6=(r(N)/10000)*2
dis pvalue6

sum coef_reg7 if coef_reg7<-0.020
gen pvalue7=(r(N)/10000)*2
dis pvalue7

sum coef_reg8 if coef_reg8<-0.058
gen pvalue8=(r(N)/10000)*2
dis pvalue8


********************************************************************************
*Graph 
********************************************************************************
*Rural 
hist coef_reg1, color(eltblue) lcolor(ebblue) xline(-0.084, lcolor(red)) title("Distribution of Coefficients from Permutation Test" "Rural", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2013") note("P-value = 0.0002" "N(10000)" "Vertical line: The true coefficient, -0.084") 

graph save "Figures\figure3A.gph", replace
graph export "Figures\figure3A.jpeg", as(jpg) quality(100) replace

hist coef_reg2, color(eltblue) lcolor(ebblue) xline(-0.077, lcolor(red))   title("Distribution of Coefficients from Permutation Test" "Rural", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2014") note("P-value = 0.0018" "N(10000)" "Vertical line: The true coefficient: -0.077") 

graph save "Figures\figure3B.gph", replace
graph export "Figures\figure3B.jpeg", as(jpg) quality(100) replace


hist coef_reg3, color(eltblue) lcolor(ebblue) xscale(range(-0.015 0.01)) xline(-0.013, lcolor(red))  xlabel(-0.013 "True coeff= -0.161" -0.01 0 0.01, labsize(small))   title("Distribution of Coefficients from Permutation Test" "Rural", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2015") note("P-value = 0.000" "N(10000)" "Vertical line: The true coefficient -0.161 (outside the range)") 

graph save "Figures\figure3C.gph", replace
graph export "Figures\figure3C.jpeg", as(jpg) quality(100) replace


hist coef_reg4, color(eltblue) lcolor(ebblue) xline(-0.104, lcolor(red)) title("Distribution of Coefficients from Permutation Test" "Rural", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2016") note("P-value = 0.0004" "N(10000)" "Vertical line: The true coefficient -0.104") 

graph save "Figures\figure3D.gph", replace
graph export "Figures\figure3D.jpeg", as(jpg) quality(100) replace

*Urban 
hist coef_reg5, color(eltblue) lcolor(ebblue) xline(0.023, lcolor(red)) title("Distribution of Coefficients from Permutation Test" "Urban", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2013") note("P-value = 0.3378" "N(10000)" "Vertical line: The true coefficient 0.023") 

graph save "Figures\figure3E.gph", replace
graph export "Figures\figure3E.jpeg", as(jpg) quality(100) replace


hist coef_reg6, color(eltblue) lcolor(ebblue) xline(0.027, lcolor(red)) title("Distribution of Coefficients from Permutation Test" "Urban", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2014") note("P-value = 0.2812" "N(1000)" "Vertical line: The true coefficient 0.027") 

graph save "Figures\figure3F.gph", replace
graph export "Figures\figure3F.jpeg", as(jpg) quality(100) replace

hist coef_reg7, color(eltblue) lcolor(ebblue) xscale(range(-0.014 0.01)) xline(-0.013, lcolor(red))  xlabel(-0.013 "True coeff= -0.020" -0.01 0 0.01, labsize(small)) title("Distribution of Coefficients from Permutation Test" "Urban", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2015") note("P-value = 0.000" "N(1000)" "Vertical line: The true coefficient -0.020 (outside the range)")

graph save "Figures\figure3G.gph", replace
graph export "Figures\figure3G.jpeg", as(jpg) quality(100) replace

hist coef_reg8, color(eltblue) lcolor(ebblue) xline(-0.058, lcolor(red)) title("Distribution of Coefficients from Permutation Test" "Urban", size(medium)) xtitle("Coefficients of {&Delta}EMW in 2016") note("P-value = 0.0224" "N(10000)" "Vertical line: The true coefficient") 

graph save "Figures\figure3H.gph", replace
graph export "Figures\figure3H.jpeg", as(jpg) quality(100) replace

